const {
  $util,
} = getApp();
Page({
  data: {
    page: 1,
    list: [],
    tabList: [],
    hasMore: true,
    keyWords: '',
    isSaaS: true,
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  onLoad(opts) {
    this.setData(opts, () => {
      this.loadTabBarList();
      opts.barTitle && wx.setNavigationBarTitle({
        title: decodeURIComponent(opts.barTitle),
      });
    });
  },
  loadTabBarList() {
    $util.request({
      url: 'entry/wxapp/edu.affairs.main',
      data: {},
      success: (res) => {
        this.setData({
          tabList: res.navlist,
          cid: res.navlist[0].cid,
        }, () => {
          this.loadList();
        });
      },
    });
  },
  loadList() {
    const D = this.data;
    let _data = {};
    if (D.keyWords && D.keyWords.trim() !== '') {
      _data = {
        keywords: D.keyWords,
      };
    } else {
      _data = {
        type: D.cid,
      };
    }
    $util.request({
      url: 'entry/wxapp/edu.affairs.more',
      data: {
        page: D.page,
        ..._data,
      },
      success: (res) => {
        let list = res.list;
        if (D.page > 1) {
          list = [...D.list, ...list];
        }
        this.setData({
          list,
          hasMore: list.length < Number(res.total),
          loaded: true,
        });
      },
    });
  },
  loadMore() {
    if (!this.data.hasMore) return;
    this.setData({
      page: this.data.page + 1,
    }, () => {
      this.loadList();
    });
  },
  onSwitchTab(e) {
    this.setData({
      cid: e.detail.obj.cid,
      page: 1,
    }, () => {
      this.loadList();
    });
  },
  onSearch(e) {
    this.setData({
      keyWords: e.detail.keyWords,
      page: 1,
    }, () => {
      this.loadList();
    });
  },
  onShareAppMessage() {
    let path = '/ws_party_const/pages/education/routine/index';
    let res = {
      path,
    };
    return res;
  },
});
